﻿Imports System.Data.SqlClient
Imports DMC.Land.User

Public Class frmTachPhuong
    Dim app As New Object
    Private strNamIn As Int16 = 0
    Private strConnection As String = ""
    Public User As New clsUser
    Private strUsername As String = ""
    Private strDVHC() As Int32
    Private strMaQuyen As String = ""
    Dim conn = New SqlConnection
    Private sqlConnect As New SqlConnection
    Private strMaChu As String = ""
    Public Property Get_MaChu() As String
        Get
            Return strMaChu
        End Get
        Set(ByVal value As String)
            strMaChu = value
        End Set
    End Property

    Public Property MaQuyen() As String
        Get
            Return strMaQuyen
        End Get
        Set(ByVal value As String)
            strMaQuyen = value
        End Set
    End Property
    Public Property DVHC() As Int32()
        Get
            Return strDVHC
        End Get
        Set(ByVal value As Int32())
            strDVHC = value
        End Set
    End Property
    Public Property UserName() As String
        Get
            Return strUsername
        End Get
        Set(ByVal value As String)
            strUsername = value
        End Set
    End Property
    Public Property Connection() As String
        Get
            Return strConnection
        End Get
        Set(ByVal value As String)
            strConnection = value
        End Set
    End Property
    Public Sub ShowDVHC()
        conn.ConnectionString = strConnection
        Try
            conn.Open()
        Catch ex As Exception
            MessageBox.Show("Lỗi kết nối" & ex.Message)
        End Try
        CtrChonDonViHanhChinh1.Get_arrMaDVHC = strDVHC  'frmDangNhap.UcKetNoi1.Get_MaDVHC ' User.Get_MaDVHC
        CtrChonDonViHanhChinh1.Get_Maquyen = strMaQuyen 'frmDangNhap.UcKetNoi1.Get_MaQuyen ' User.Get_MaQuyen
        CtrChonDonViHanhChinh1.Get_sqlConnect = conn
        CtrChonDonViHanhChinh1.HienThiThongTinLenTreeview()
    End Sub
    Private Sub frmTachPhuong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
    End Sub

    Private Sub cmdThucHien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThucHien.Click
        If (CtrChonDonViHanhChinh1.Current_DVHC = 0) Then
            MessageBox.Show("Hãy chọn đơn vị hành chính của phường.", "In sổ cấp GCN", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If
        Try
Loi:
            Dim strSql As String = ""
            strSql = strSql + "declare @MaDonVihanhChinh nvarchar(20)" + vbNewLine
            strSql = strSql + "set @MaDonVihanhChinh = '" & CtrChonDonViHanhChinh1.Current_DVHC & "'" + vbNewLine
            strSql = strSql + "declare @MaHoSoCapGCN nvarchar(20)" + vbNewLine
            strSql = strSql + "declare @MaThuaDatCapGCN nvarchar(20)" + vbNewLine
            strSql = strSql + "declare @MaXa nvarchar(10)" + vbNewLine
            strSql = strSql + "set @maXa = (select MaXa from tblTuDienDonVIHanhChinh where madonvihanhchinh = @MaDonVihanhChinh)" + vbNewLine
            strSql = strSql + "delete from tblHoSoCapGCN where maxa <> @maXa" + vbNewLine
            strSql = strSql + "delete from tblThuaDatCapGCN where MaDonVihanhChinh <> @MaDonVihanhChinh" + vbNewLine
            strSql = strSql + "delete from tblDangKyBienDong where MaHoSoCapGCN not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine
            strSql = strSql + "delete from tblMucDichSuDungDat where MaThuaDatCapGCN not in (select MaThuaDatCapGCN from tblThuaDatCapGCN )" + vbNewLine
            strSql = strSql + "delete from tblnguonGocSuDungDat where MaThuaDatCapGCN not in (select MaThuaDatCapGCN from tblThuaDatCapGCN )" + vbNewLine
            strSql = strSql + "delete from tblChuHoSoCapGCN where MaHoSoCapGCN not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine
            strSql = strSql + "delete from tblHoSoThamDinh where MaHoSoCapGCN not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine
            strSql = strSql + "delete from tblHoSoTiepNhan where MaHoSoCapGCN  not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine
            strSql = strSql + "delete from tblHoSoPheDuyet where MaHoSoCapGCN not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine
            strSql = strSql + "delete from tblHoSoCapGCN where MaHoSoCapGCN not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine
            strSql = strSql + "delete from tblHoiDongXetDuyet where MaHoSoCapGCN  not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine
            strSql = strSql + "delete from tblHoSoTrinhDiaChinh where MaHoSoCapGCN  not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine
            strSql = strSql + "delete from tblDanhSachToaDo where MaHoSoCapGCN  not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine
            strSql = strSql + "delete from tblFileHSKT where MaHoSo  not in (select MaHoSoCapGCN from tblThuaDatCapGCN)" + vbNewLine

            Dim cmdIns As New SqlCommand

            Dim sqlconn As New SqlConnection
            If sqlconn.State = ConnectionState.Closed Then
                sqlconn.ConnectionString = strConnection

                Try
                    sqlconn.Open()
                Catch ex As Exception
                    MessageBox.Show("Lỗi kết nối" & ex.Message)
                End Try
            End If
            cmdIns.CommandType = CommandType.Text
            cmdIns.Connection = sqlconn
            cmdIns.CommandText = strSql
            cmdIns.ExecuteNonQuery()
            cmdIns.Dispose()
            MessageBox.Show("Hoàn Thành", "DMCLand", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            GoTo Loi
            MessageBox.Show("Phát sinh lỗi " + vbNewLine + ex.Message, "DMCLand", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
       

    End Sub
End Class